﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TYPLC.PLC
{
    public abstract class IPLCBase<T> : IDisposable
    {
        internal bool IsRunning = true;
        internal Dictionary<string, T> dicAddress = new Dictionary<string, T>();
        public abstract bool Connect();

        public bool Write(string address, object value)
        {
            if (AddressAs(address, out T data))
            {
                dicAddress[address] = data;
                return WritePri(data, value);
            }
            return false;
        } 
        public bool Read(string address, out object value)
        {
            value = default;
            if (AddressAs(address, out T data))
            {
                dicAddress[address] = data;
                return ReadPri(data, out value);
            }
            return false;
        }

        public abstract bool WritePri(T address, object value);

        public abstract bool ReadPri(T address, out object value);
        public bool CheckAddress(string address)
        {
            if (AddressAs(address, out T data))
            {
                dicAddress[address] = data;
                return true;
            }
            return false;
        }

        public abstract bool AddressAs(string address, out T data);

        public void Dispose()
        {
            IsRunning = false;
        }
    } 
}
